package com.friday.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.friday.dao.DeliveryDao;
import com.friday.dao.InvoiceDao;
import com.friday.dao.OrderDao;
import com.friday.dao.QuotationDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class OrderController {

    @Autowired
    private OrderDao orderDao;
    @Autowired
    private QuotationDao quotationDao;
    @Autowired
    private DeliveryDao deliveryDao;
    @Autowired
    private InvoiceDao  invoiceDao;


    @RequestMapping("/orderList")
    public JSONArray getAllOrder(){
        return orderDao.getAllOrder();
    }

    @RequestMapping("/orderAgentList")
    public JSONArray getAgentOrder(@RequestParam("id") int id){
        return orderDao.getAgentOrder(id);
    }

    @RequestMapping("/orderInvoiceList")
    public JSONArray  getOrderForInvoice(){
        return orderDao.getOrderForInvoice();
    }



    @RequestMapping("/orderDetail")
    public JSONObject getOrderDlt(@RequestParam("id") int id){
        JSONObject order=orderDao.getOrderById(id);
        JSONArray orderdtl =orderDao.getOrderDtlByOId(id);
        for(int i=0;i<orderdtl.size();i++){
            orderdtl.getJSONObject(i).put("key",i);
        }
        order.put("goods",orderdtl);
        return order;
    }

    @RequestMapping("/orderAgentDetail")
    public JSONObject getOrderAgentDtl(@RequestParam("id") int id){
        JSONObject order=orderDao.getOrderById(id);
        JSONArray orderdtl =orderDao.getOrderDtlByOId(id);
        for(int i=0;i<orderdtl.size();i++){
            orderdtl.getJSONObject(i).put("key",i);
        }
        //商品信息
        order.put("goods",orderdtl);
        //发货信息
        order.put("delivery",deliveryDao.getDeliRecordByOId(id));
        return order;
    }

    @RequestMapping("/orderSend")
    public String sendOrder(@RequestParam("id") int order_id){
        int tag=orderDao.changeStatus(order_id,"待发货");
        if(tag>0)
            return "success";
        return "fail";
    }

    @RequestMapping("/orderPay")
    public String orderPay(@RequestParam("id") int order_id){
        //修改order状态为已完成
        int tag1= orderDao.changeStatus(order_id,"已完成");
        //生效发票
        int tag2= invoiceDao.effectInvoice(order_id);
        if(tag1>0 && tag2>0)
            return "success";
        return "fail";
    }


    @RequestMapping("/orderDelete")
    public String delOrderById(@RequestParam("id") int id){
        JSONObject order=orderDao.getOrderById(id);
        int tag1 = orderDao.delOrderById(id);
        int tag2 = orderDao.delOrderDtlByOId(id);
        //若订单对应的有报价单，则报价单状态回退为已通过
        if(order.getInteger("quotation_id")!=null){
            quotationDao.changeStatus(order.getInteger("quotation_id"),"已通过");
        }
        if( tag1 > 0 ){
            return "success";
        }else{
            return "fail";
        }
    }

    @RequestMapping("/orderAdd")
    public JSONObject addOrder(@RequestBody JSONObject order){
        JSONObject result = new JSONObject();
        //预置错误
        result.put("result","fail");
        result.put("order_id",null);
        //添加order
        int tag1= orderDao.addOrder(order);
        if(tag1 >0 ){
            int tag2=0;
            JSONArray orderdtl =order.getJSONArray("goods");
            int order_id = order.getInteger("id");
            //添加orderdtl
            for(int i=0;i<orderdtl.size()-1;i++){
                orderdtl.getJSONObject(i).put("order_id",order_id);
                tag2+=orderDao.addOrderDtl(orderdtl.getJSONObject(i));
            }
            if(tag2 == orderdtl.size()-1){
                result.put("result","success");
                result.put("order_id", order_id);
                //订单依据报价单创建，其状态为已完成
                if(order.getInteger("quotation_id")!=null){
                    quotationDao.changeStatus(order.getInteger("quotation_id"),"已完成");
                }
            }
        }
        return result;
    }







}
